package com.example.myapplication.douyinminiappscrolldemo

import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.viewinterop.AndroidView

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            WebViewWithButton()
        }
    }
}

@Composable
fun WebViewWithButton() {
    val context = LocalContext.current
    var scrollY by remember { mutableStateOf(0) }

    Box(modifier = Modifier.fillMaxSize()) {
        AndroidView(
            factory = {
                WebView(it).apply {
                    webViewClient = WebViewClient()
                    settings.javaScriptEnabled = true
                    loadUrl("https://weibo.com")
                    setOnScrollChangeListener { _, _, y, _, _ ->
                        scrollY = y
                    }
                }
            },
            modifier = Modifier.fillMaxSize()
        )

        Button(
            onClick = { /* 处理按钮点击事件 */ },
            modifier = Modifier
                .align(Alignment.Center)
                .offset { IntOffset(0, -scrollY) }
        ) {
            Text("跟随滑动的按钮")
        }
    }
}